package com.fire.control.system.module.system.controller.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fire.control.system.dto.ResourceDTO;
import com.fire.control.system.entity.Resource;
import com.fire.control.system.entity.Role;
import com.fire.control.system.entity.User;
import com.fire.control.system.module.common.controller.BaseController;
import com.fire.control.system.module.role.service.IRoleService;
import com.fire.control.system.module.system.service.IAccountManagerService;
import com.fire.control.system.module.user.dao.UserRepository;
import com.fire.control.system.module.user.service.IUserService;
import com.fire.control.system.standard.BusinessInterfaceStandard;
import com.fire.control.system.statics.ResponseMessage;
import com.fire.control.system.support.protocol.account.AccountRequestProtocol;
import com.fire.control.system.support.protocol.comman.ProtocolSelector;
import com.fire.control.system.support.protocol.comman.RequestProtocol;
import com.fire.control.system.support.protocol.comman.ResponseProtocol;
import com.fire.control.system.support.protocol.login.LoginRequestProtocol;
import com.fire.control.system.support.protocol.sys.SystemRequestProtol;
import com.fire.control.system.support.util.ResourceTreeUtil;

/**
 * @author liuxincheng
 * @ClassName: AccountManagerControl
 * @Description: TODO(账户控制器)
 * @date 2017年5月20日 下午5:03:26
 */
@RestController
@RequestMapping(path = "/web/accountCtr")
public class AccountManagerControl extends BaseController implements BusinessInterfaceStandard<RequestProtocol, ResponseProtocol> {

	@Autowired
	private IAccountManagerService accountManagerService;

	@Autowired
	private ProtocolSelector protocolSelector;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private IRoleService roleService;
	/**
	 * @author liuxincheng
	 * @Title: getCountList
	 * @Description: TODO(获得账号列表)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getCountList")
	public ResponseProtocol getCountList(AccountRequestProtocol requestProtocol, Pageable p) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.getCountList(requestProtocol, p);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getCountListByCondition
	 * @Description: TODO(通过条件获得账号列表)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getCountListByCondition", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getCountListByCondition(AccountRequestProtocol requestProtocol, Pageable p) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.getCountListByCondition(requestProtocol, p);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: saveAccount
	 * @Description: TODO(添加管理员)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "saveAccount", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol saveAccount(AccountRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.saveAccount(requestProtocol,this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: updateAccount
	 * @Description: TODO(修改账户信息)
	 * @param @param requestProtocol
	 * @param @return    设定文件
	 * @return ResponseProtocol    返回类型
	 * @throws null
	 */
	@RequestMapping(path = "updateAccount", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol updateAccount(AccountRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.updateAccount(requestProtocol,this.getUser());
		return response;
	}
	
	/**
	 * @author liuxincheng
	 * @Title: deleteAccount
	 * @Description: TODO(删除账户信息)
	 * @param @param requestProtocol
	 * @param @return    设定文件
	 * @return ResponseProtocol    返回类型
	 * @throws
	 */
	@RequestMapping(path = "deleteAccount", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol deleteAccount(AccountRequestProtocol requestProtocol) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.deleteAccount(requestProtocol,this.getUser());
		return response;
	}
	/**
	 * @author liuxincheng
	 * @Title: saveRole
	 * @Description: TODO(用户分配角色)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "userAssignRoles", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol userAssignRoles(AccountRequestProtocol requestProtocol) {
		ResponseProtocol response = accountManagerService.userAssignRoles(requestProtocol,this.getUser());
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: getLogList
	 * @Description: TODO(获得操作日志列表)
	 * @param @param
	 *            requestProtocol
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getLogList", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getLogList(AccountRequestProtocol requestProtocol, Pageable p) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.getLogList(requestProtocol, p);
		return response;
	}

	/**
	 * @author liuxincheng
	 * @Title: gerLogsByCondition
	 * @Description: TODO(通过条件获得日志列表)
	 * @param @param
	 *            requestProtocol
	 * @param @param
	 *            p
	 * @param @return
	 *            设定文件
	 * @return ResponseProtocol 返回类型
	 * @throws null
	 */
	@RequestMapping(path = "gerLogsByCondition", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol gerLogsByCondition(AccountRequestProtocol requestProtocol, Pageable p) {
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = accountManagerService.gerLogsByCondition(requestProtocol, p);
		return response;
	}
	
	/**
	 * @author  liuxincheng
	 * @Title: modifyPassword
	 * @Description: TODO(修改用户密码)
	 * @param @param requestProtocol
	 * @param @return    设定文件
	 * @return ResponseProtocol    返回类型
	 * @throws null
	 */
	@RequestMapping(path="modifyPassword",method=RequestMethod.POST)
	public ResponseProtocol modifyPassword(SystemRequestProtol requestProtocol){
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = userService.modifyPassword(requestProtocol,this.getUser());
		return response;
	}
	
	/**********************************************模块辅助业务接口**************************************************/
	
	/**
	 * @author liuxincheng
	 * @Title: getCurrentRoles
	 * @Description: TODO(获取当前登陆人员的所属角色)
	 * @param @param requestProtocol
	 * @param @return    设定文件
	 * @return ResponseProtocol    返回类型
	 * @throws null
	 */
	@RequestMapping(path="getCurrentRoles",method=RequestMethod.POST)
	public ResponseProtocol getCurrentRoles(SystemRequestProtol requestProtocol){
		requestProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = userService.getCurrentRoles(requestProtocol,this.getUser());
		return response;
	}
	
	/**
	 * @author liuxincheng
	 * @Title: getResourcesByUser
	 * @Description: TODO(获取当前登陆人员的菜单)
	 * @param @param reqsProtocol
	 * @param @return
	 * @param @throws Exception    设定文件
	 * @return ResponseProtocol    返回类型
	 * @throws null
	 */
	@RequestMapping(path = "getResourcesByUser", method = RequestMethod.POST)
	@ResponseBody
	public ResponseProtocol getResourcesByUser(LoginRequestProtocol reqsProtocol) throws Exception {
		reqsProtocol.setTaskuuid(UUID.randomUUID().toString());
		ResponseProtocol response = protocolSelector.choose(reqsProtocol);
		
		List<String> roleIds = new ArrayList<String>();
		
		User u = userRepository.findOne(this.getUser().getUserId());
		System.out.println(this.getUser().getUserId());
		for(Role r : u.getRole()){
			roleIds.add(r.getId());
		}
		
		Set<Resource> resources = roleService.getResourceByRoleId(roleIds);
		List<ResourceDTO> resourceList = null;
		//List<ResourceThreeDTO> resourceThreeDTO = new ArrayList<ResourceThreeDTO>();
		if(resources!=null && !resources.isEmpty()){
			//resourceThreeDTO = ResourceTreeUtil.resourceDTOTree(resources);
			resourceList = ResourceTreeUtil.resourceTree(resources);
		}
		if(!resourceList.isEmpty() && resourceList.size()>0){
			response.setData(resourceList);
			ResponseMessage.CODE_200.messageToProtocol(response).setMessage("数据返回成功!");
		}else{
			ResponseMessage.CODE_400.messageToProtocol(response).setMessage("数据返回失败!");
		}
		return response;
	}
}
